﻿using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Coldairarrow.Business.PB
{
    public class PB_LanewayBusiness : BaseBusiness<PB_Laneway>, IPB_LanewayBusiness, ITransientDependency
    {
        readonly IOperator _operator;
        public PB_LanewayBusiness(IDbAccessor db, IOperator @operator)
            : base(db)
        {
            _operator = @operator;
        }

        #region 外部接口
        
        public async Task<PageResult<PB_LanewayDTO>> GetDataListAsync(PageInput<PB_LanewayPageInput> input)
        {
            var search = input.Search;
            Expression<Func<PB_Laneway, Base_Department, PB_LanewayDTO>> select = (a, b) => new PB_LanewayDTO
            {
                StorName = b.Name,
            };
            select = select.BuildExtendSelectExpre();
            var q = from a in GetIQueryable().AsExpandable()
                    join b in Db.GetIQueryable<Base_Department>() on a.StorId equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    select @select.Invoke(a, b);
            //if (!search.StorId.IsNullOrEmpty())
            //{
            //    var keyword = $"%{search.Search.Keyword}%";
            //        q = q.Where(x =>
            //              EF.Functions.Like(x.StorName, keyword));
            //}
            if (!search.StorId.IsNullOrEmpty())
            {
                q = q.Where(x => x.StorId == search.StorId );
            }
            if (!search.Search.Keyword.IsNullOrEmpty())
            {
                var keyword = $"%{search.Search.Keyword}%";
                q = q.Where(x =>
                          EF.Functions.Like(x.Name, keyword) || EF.Functions.Like(x.Code, keyword))  ;
            }
            return await q.GetPageResultAsync(input);
        }

        public async Task<PB_Laneway> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        [DataAddLog(UserLogType.巷道管理, "Name", "巷道")]
        [DataRepeatValidate(new string[] { "StorId", "Name" }, new string[] { "仓库", "巷道" }, allData: false, matchOr: false)]
        public async Task AddDataAsync(PB_Laneway data)
        {
            await InsertAsync(data);
        }

        [DataEditLog(UserLogType.巷道管理, "Name", "巷道")]
        [DataRepeatValidate(new string[] { "StorId", "Name" }, new string[] { "仓库", "巷道" }, allData: false, matchOr: false)]
        public async Task UpdateDataAsync(PB_Laneway data)
        {
            await UpdateAsync(data);
        }

        [DataDeleteLog(UserLogType.巷道管理, "Name", "巷道")]
        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        public async Task<List<PB_Laneway>> QueryLanewayDataAsync()
        {
            var q = GetIQueryable();

            return await q.ToListAsync();
        }

        public async Task<List<PB_Laneway>> GetDataListAsync(string storId)
        {
            var q = GetIQueryable();
            q = q.Where(w => w.StorId == storId);

            return await q.ToListAsync();
        }

        #endregion

        #region 私有成员

        #endregion
    }
}